package com.chanjiedata.changbs.module.mine;

/**
 * 文件名 : MessageActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/28 14:43
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.google.android.material.tabs.TabLayout;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MessageActivity extends BaseActivity {


    Unbinder unbinder;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    String[] titles = {"提现", "奖励", "订单", "系统"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_tab_viewpager);
        StatusBarUtil.setRootViewFitsSystemWindows(this, false);
        StatusBarUtil.setTranslucentStatus(this);
        unbinder = ButterKnife.bind(this);
        tvTitle.setText("消息");
        backIv.setOnClickListener(view -> finish());
        initView();

    }

    protected void initView() {
       
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(MessageTypeFragment.NewInstance(0));
        fragmentList.add(MessageTypeFragment.NewInstance(1));
        fragmentList.add(MessageTypeFragment.NewInstance(2));
        fragmentList.add(MessageTypeFragment.NewInstance(3));
        viewPager.setOffscreenPageLimit(4);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }

        });
        initTabLayout();
    }
    
    

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    //设置tablayout
    private void initTabLayout(){

        tabLayout.setupWithViewPager(viewPager);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab == null || tab.getText() == null) {
                    return;
                }
                scaleAnima(1.0f, 1.3f, tab.view);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab.view == null || tab.getText() == null) {
                    return;
                }
                scaleAnima(1.3f, 1.0f, tab.view);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        scaleAnima(1.0f, 1.3f, tabLayout.getTabAt(0).view);
    }

    private void scaleAnima(float v, float v2, TabLayout.TabView title) {
        AnimationSet animationSet = new AnimationSet(true);
        ScaleAnimation scaleAnimation = new ScaleAnimation(
                v, v2, v, v2,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(100);//动画持续时间
        animationSet.addAnimation(scaleAnimation);//保存动画效果到。。
        animationSet.setFillAfter(true);//结束后保存状态
        title.startAnimation(animationSet);//设置给控件

    }
}
